/**
 *
 * Created by Song,YeWen, 9/15/2011 5:18:42 PM
 *
 */

function validateVideoUrl(ori_url) 
{
    //We use a fake constant duration for xtube.com, because it is adult video, 
    //TODO: FIXME
    var fcduration = '100';
	const URL_POST_XML = 'http://www.xtube.com/find_video.php';	
	const URL_POST_XML_PARAMS = 'user_id=%1&clip_id=%2&video_id=%3';
	var html = download_page(ori_url);
	// get subdomain
	//var swfUrl   = copyBetween(html, 'so_s.addVariable("swfURL", "', '"');
	var user_id  = copyBetween(html, 'so_s.addVariable("user_id", "', '"');
	var video_id = copyBetween(html, 'so_s.addVariable("video_id", "', '"');
	var clip_id  = copyBetween(html, 'so_s.addVariable("clip_id", "', '"');
    if (!video_id ) {
	     // vid not found
	     return 'NotFoundVideo';
	}
    // get xml
	var xml_post_param = strFormat(URL_POST_XML_PARAMS,user_id, clip_id, video_id);
	var xml = download_page_post(URL_POST_XML, xml_post_param);
	// get video url
	var urlenc_string = copyBetween(xml, '&filename=', '&');
	var flv_string = clean_url(urlenc_string);
    var sb = [];
      
    sb.push(flv_string);
    if(sb.length){
       //var matches = html.match(/<title>(.*?)<\/title>/);
       var matches = html.match(/<title>([\s\S]*?)<\/title>/);
       if(matches){
    	   return 'success_title:' + encodeURIComponent(matches[1])
    	          + '\n video_urls:' + encodeURIComponent(sb)
    	          + '\n seconds:' + Math.round(parseInt(fcduration));
       }
    }
	return 'error:empty_path';
}

/* http://www.xtube.com/paid_channels/play.php?v=hKxvgwnuTJv&dv=JHtLwlZe25P&cl=2bsngmlBAj_&view_type=preview */
function js_get_media_url(ori_url) 
{
	const URL_POST_XML = 'http://www.xtube.com/find_video.php';	
	const URL_POST_XML_PARAMS = 'user_id=%1&clip_id=%2&video_id=%3';
	var html = download_page(ori_url);
	// get subdomain
	//var swfUrl   = copyBetween(html, 'so_s.addVariable("swfURL", "', '"');
	var user_id  = copyBetween(html, 'so_s.addVariable("user_id", "', '"');
	var video_id = copyBetween(html, 'so_s.addVariable("video_id", "', '"');
	var clip_id  = copyBetween(html, 'so_s.addVariable("clip_id", "', '"');
	// get xml
	var xml_post_param = strFormat(URL_POST_XML_PARAMS,user_id, clip_id, video_id);
	var xml = download_page_post(URL_POST_XML, xml_post_param);
	// get video url
	var urlenc_string = copyBetween(xml, '&filename=', '&');
	
	/*
	//urlencode to ascii by JS native method
	var flv_string = decodeURIComponent(urlenc_string);
	*/

	var flv_string = clean_url(urlenc_string);

	//flv_string = swfUrl + flv_string;

	return flv_string;
}
